from typing import Dict
from x_com.import_api import ecommerce_data_source_service_0_0_1
from utils import time_utils



# 销售统计数据
class ProductSaleDataAnalyzer:
    # 年度数据
    class YearData:
        # 年度销售额
        revenu: float
        # 年度销量
        volume: int
        # 月度销售额
        monthly_revenu_dict: Dict[int, float]
        # 月度销售
        monthly_volume_dict: Dict[int, int]
        # 产品销售额
        product_revenu_dict: Dict[str, float]
        # 产品销量
        product_volume_dict: Dict[str, int]
        # 产品销售额占比
        product_revenu_share_dict: Dict[str, float]
        # 产品销量占比
        product_volume_share_dict: Dict[str, float]
        def __init__(self) -> None:
            self.revenu = 0.0
            self.volume = 0
            self.monthly_revenu_dict = {}
            self.monthly_volume_dict = {}
            self.product_revenu_dict = {}
            self.product_volume_dict = {}
            # 可以修改为获取的时候计算
            self.product_revenu_share_dict = {}
            self.product_volume_share_dict = {}
    # 年度销售额数据
    # key 为 年份
    year_data_dict : Dict[int, YearData]

    def __init__(self) -> None:
        self.year_data_dict = {}

    def data_in(self, param: ecommerce_data_source_service_0_0_1.ProductSaleInfo):
        # 转换为事件类型的数据
        datetime_obj = time_utils.str_to_datetime(param.buy_time)

        # 数据所在的年份
        year = datetime_obj.year

        # 计算年度销售额
        # 判断当前年份是否有数据
        if year not in self.year_data_dict:
            self.year_data_dict[year] = self.YearData()

        year_data = self.year_data_dict[year]

        #  单价 * 数量，为当前产品的总价
        year_data.revenu +=  param.price * param.num
        year_data.volume +=  param.num

        month = datetime_obj.month
        # 统计月度销售额和销量表
        if month  not in year_data.monthly_revenu_dict:
            year_data.monthly_revenu_dict[month] = param.price * param.num
        else:
            year_data.monthly_revenu_dict[month] += param.price * param.num

        if month  not in year_data.monthly_volume_dict:
            year_data.monthly_volume_dict[month] = param.num
        else:
            year_data.monthly_volume_dict[month] += param.num
        # 按照分类统计产品的销售额和销量
        if param.product_category not in year_data.product_revenu_dict:
            year_data.product_revenu_dict[param.product_category] = param.price * param.num
        else:
            year_data.product_revenu_dict[param.product_category] += param.price * param.num
        # 
        if param.product_category not in year_data.product_volume_dict:
            year_data.product_volume_dict[param.product_category] = param.num
        else:
            year_data.product_volume_dict[param.product_category] += param.num

        # 数据跟新的时候，所有的产品占比都要重新计算
        for category, revenu in year_data.product_revenu_dict.items():
            year_data.product_revenu_share_dict[category] = revenu / year_data.revenu

        for category, volume in year_data.product_volume_dict.items():
            year_data.product_volume_share_dict[category] = volume / year_data.volume

    def get_data_by_year(self, year: int) -> YearData:
        return self.year_data_dict.get(year)








       



